package com.sdxb.springbootexample.controller;

import com.sdxb.springbootexample.entity.User;
import com.sdxb.springbootexample.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;

@Controller

public class UserController {
    // 注入UserService
    @Resource
    private UserService userService;

    /*@RequestMapping(value = "/login")
    public String login(User user) {
        ModelAndView mv = new ModelAndView();
        user = userService.get(user);

        if (user != null) {
            mv.addObject("user", user);
            return "user";
        } else {

            return "login";
        }
    }*/


    @RequestMapping(value = "/login")
        public String login(String userid , String password,Model model) {
        ModelAndView mv = new ModelAndView();
        User user = userService.get(userid);
        System.out.println("---------"+userid);
        System.out.println("---------"+user);

        if (userid != null && user.getUserid().equals(userid)) {
            if (user.getPassword().equals(password)) {
                System.out.println("密码正确，登录成功！");
                mv.addObject("user", user);
                model.addAttribute("msg1","请输入正确密码");
                return "user";
            } else {
                System.out.println("密码错误，登陆失败！");
                model.addAttribute("msg1","请输入正确密码");

               return "redirect:/error";
            }
        } else {
            System.out.println("用户不存在，请重新输入！");
            return "login";


        }
    }






    @RequestMapping("/register")
    public String Regist(User user,Model model){
       System.out.println("注册用户："+user.getUserid()+user.getPassword());
    userService.regist(user);
         model.addAttribute("msg","注册成功");
            return"user" ;
      }
    @RequestMapping("/add")
    public String add(){

        return "add";
    }

    @RequestMapping("/delete")
    public String delet(String userid,Model model){
    userService.deletebyid(userid);
    model.addAttribute("userid","删除成功");
        return "user";
    }

}